//
//  VRSafeKnowlegeDetailViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/14.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRSafeKnowlegeDetailViewController.h"

#import "VRSafetyKnowledgeModel.h"

@interface VRSafeKnowlegeDetailViewController ()

@property (nonatomic, strong) UIScrollView *contentScrollView1;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *detailLbl;

@property (nonatomic, strong) VRSafetyKnowledgeModel *model;

@end

@implementation VRSafeKnowlegeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = self.titleStr.length > 0 ? self.titleStr : @"安全知识详情";
    
    [self setupSubView];
    [self getSafeKnowlegeDetail];
}

- (void)setupSubView
{
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = RGB(238, 238, 238);
    [self.contentScrollView1 addSubview:contentView];
    self.contentView = contentView;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentScrollView1);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.layer.masksToBounds = YES;
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    [contentView addSubview:imgView];
    self.imgView = imgView;
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(156);
    }];
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.numberOfLines = 0;
    detailLbl.textColor = RGB(80, 80, 80);
    detailLbl.font = VRFont(14);
    [contentView addSubview:detailLbl];
    self.detailLbl = detailLbl;
    [detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imgView.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(detailLbl);
    }];
}


- (void)refreshPageWith:(VRSafetyKnowledgeModel *)model
{
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.fPicture]];
    self.detailLbl.text = model.fContent;
}


#pragma mark - networking
- (void)getSafeKnowlegeDetail
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //    param[@"UserID"] = [VRUser shareUser].token;
    param[@"learningPopularityID"] = self.fID;
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [JKNetworking postWithSign:GetSafeKnowlegeDetail parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.model = [VRSafetyKnowledgeModel mj_objectWithKeyValues:responseObject[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self refreshPageWith:self.model];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
    } error:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
        });
    }];
}



#pragma mark - lazy
- (UIScrollView *)contentScrollView1
{
    if (_contentScrollView1 == nil) {
        
        _contentScrollView1 = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//        _contentScrollView1.backgroundColor = ScrollViewBgColor;
        //        _contentScrollView1.delegate = self;
        //        _contentScrollView.bounces = NO;
        _contentScrollView1.alwaysBounceVertical = YES;
        //        _contentScrollView.delegate = self;
        if (@available(iOS 11.0, *)) {
            //            _contentScrollView1.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_contentScrollView1];
    }
    return _contentScrollView1;
}

@end
